package edu.lingnan.constroller;

import edu.lingnan.common.Result;
import edu.lingnan.pojo.Product;
import edu.lingnan.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xBaozi
 * @version 1.0
 * @classname ProductController
 * @description 产品控制器
 * @date 2022/5/27 18:38
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/selectAllProduct")
    public Result selectAll() {

        List<Product> productList = productService.selectAllProduct();
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(productList);
        result.setMsg("查询成功");
        return result;
    }

     //通过商品id查询
    @RequestMapping("/selectProductById/{id}")
     public Result selectProductById(@PathVariable("id") Long id) {

         List<Product> productList = productService.selectProductById(id);

         Result result = new Result();
         result.setCode(Result.SUCCESS_CODE);
         result.setData(productList);
         result.setMsg("查询成功");
         return result;
     }

    //通过商品名称的查询
    @RequestMapping("/selectProductByName/{name}")
    public Result selectProductByName(@PathVariable("name") String name) {

        List<Product> productList = productService.selectProductByName(name);

        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(productList);
        result.setMsg("查询成功");
        return result;
    }

    //通过商品名称的模糊查询
    @RequestMapping("/selectProductLikeName/{name}")
    public Result selectProductLikeName(@PathVariable("name") String name) {

        name="%"+name+"%";
        List<Product> productList = productService.selectProductLikeName(name);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(productList);
        result.setMsg("查询成功");
        return result;
    }

    //通过商品类型查询
    @RequestMapping("/selectProductByType/{type}")
    public Result selectProductByType(@PathVariable("type") String type) {

        List<Product> productList = productService.selectProductByType(type);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(productList);
        result.setMsg("查询成功");
        return result;
    }

    //通过商铺id查询所有商品
    @RequestMapping("/selectProductByStoreId/{id}")
    public Result selectProductByStoreId(@PathVariable("id") Long id) {

        List<Product> productList = productService.selectProductByStoreId(id);

        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(productList);
        result.setMsg("查询成功");
        return result;
    }

    //通过id删除商品
    @RequestMapping("/deleteProductById/{id}")
    public Result deleteProductById(@PathVariable("id") Long id) {
        int row = productService.deleteProductById(id);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(row);
        if (row == 0) {
            result.setMsg("没有该商品");
        } else {
            result.setMsg("删除成功");
        }
        return result;
    }

    //通过id批量删除
    //请求路径  http://localhost:9090/product/deleteProductByIds?ids=6&ids=7
    @RequestMapping("/deleteProductByIds")
    public Result deleteProductByIds(Long[] ids) {

        int row = productService.deleteProductByIds(ids);

        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(row);
        if (row == 0) {
            result.setMsg("没有该商品");
        } else {
            result.setMsg("删除成功!删除"+row+"个商品");
        }
        return result;
    }

    //改变商品状态:上架、下架
    @RequestMapping("/updateProductStatus/{id}/{status}")
    public Result updateProductStatus(@PathVariable("id") Long id, @PathVariable("status") int status) {

        int row = productService.updateProductStatus(id,status);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(row);
        if (row == 0) {
            result.setMsg("没有该商品");
        } else {
            result.setMsg("商品状态修改成功");
        }
        return result;
    }

    //修改商品信息

    @RequestMapping("/updateProduct")
    public Result updateProduct(Product product) {

        int row = productService.updateProduct(product);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(row);
        if (row == 0) {
            result.setMsg("没有该商品");
        } else {
            result.setMsg("修改成功");
        }
        return result;
    }

    //添加商品
    @RequestMapping("/addProduct")
    public Result addProduct(Product product) {

        Long row = productService.addProduct(product);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(row);
        return result;
    }




}
